package com.car.advertising.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Environment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.car.advertising.R;
import com.car.advertising.connection.HttpUrl;
import com.car.advertising.util.CameraUtil;
import com.support.widget.layout.BaseToolBarActivity;
import com.support.widget.network.FileUpLoader;
import com.support.widget.util.LogUtil;
import com.support.widget.util.ToastUtil;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class CarOwnerAuthenticationActivity extends BaseToolBarActivity {

    public static String FILEPATH = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
    public static String FILENAME = "jiemi";
    public static String SUFFIX = ".png";
    private CameraUtil cameraUtil;
    private File file;
    private File jiaFile;
    private File xingFile;

    private ImageView jiaShiZhengIv;
    private ImageView xingShiZhengIv;
    private ImageView selectZhengJianIv;
    private RelativeLayout jiaShiZhengRl;
    private RelativeLayout xingShiZhengRl;
    private TextView carMsgCommitTv;

    private Dialog popupDialog1;
    private FrameLayout deleteJiaFl, deleteXingFl;

    @Override
    protected void initToolBar() {
        setTitleClickable(false);
        setSubtitleClickable(true);
        setToolBarBackgroundColor(R.color.color_white);
        setToolBarTitle("车辆证件信息");
        setToolBarTitleColor(R.color.black_bg);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_car_owner_authentication);

        jiaShiZhengIv = (ImageView) findViewById(R.id.jia_shi_zheng_iv);
        xingShiZhengIv = (ImageView) findViewById(R.id.xing_shi_zheng_iv);
        jiaShiZhengRl = (RelativeLayout) findViewById(R.id.jia_shi_zheng_rl);
        xingShiZhengRl = (RelativeLayout) findViewById(R.id.xing_shi_zheng_rl);
        carMsgCommitTv = (TextView) findViewById(R.id.car_msg_commit_tv);

        deleteJiaFl = (FrameLayout) findViewById(R.id.car_delete_jia_fl);
        deleteXingFl = (FrameLayout) findViewById(R.id.car_delete_xing_fl);

        jiaShiZhengRl.setOnClickListener(this);
        xingShiZhengRl.setOnClickListener(this);
        carMsgCommitTv.setOnClickListener(this);
        deleteJiaFl.setOnClickListener(this);
        deleteXingFl.setOnClickListener(this);

        cameraUtil = new CameraUtil(CarOwnerAuthenticationActivity.this);
    }

    private String getSystemTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyMMddHHmmss");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        return formatter.format(curDate);
    }

    @Override
    public void onCritClick(View v) {
        switch (v.getId()) {
            case R.id.jia_shi_zheng_rl:
                selectZhengJianIv = jiaShiZhengIv;
                showChangePersonalIconDialog();
                break;

            case R.id.xing_shi_zheng_rl:
                selectZhengJianIv = xingShiZhengIv;
                showChangePersonalIconDialog();
                break;

            case R.id.car_delete_jia_fl:
                deleteJiaFl.setVisibility(View.GONE);
                jiaShiZhengRl.setVisibility(View.VISIBLE);
                jiaShiZhengIv.setVisibility(View.GONE);
                break;

            case R.id.car_delete_xing_fl:
                deleteXingFl.setVisibility(View.GONE);
                xingShiZhengRl.setVisibility(View.VISIBLE);
                xingShiZhengIv.setVisibility(View.GONE);
                break;

            case R.id.car_msg_commit_tv:
                if (jiaFile == null) {
                    ToastUtil.show(CarOwnerAuthenticationActivity.this, "请拍摄照片", Toast.LENGTH_SHORT);
                    return;
                }
                uploadIcon(jiaFile.toString());
                break;

            case R.id.btn_take_photo:
                FILENAME = getSystemTime() + "jiemi";
                file = new File(FILEPATH + "/" + FILENAME + SUFFIX);
                cameraUtil.openCamera(false, new CameraUtil.ResultCallback() {
                    @Override
                    public void getResult(File saveFile) {
                        selectZhengJianIv.setVisibility(View.VISIBLE);
                        if (selectZhengJianIv == xingShiZhengIv) {
                            deleteXingFl.setVisibility(View.VISIBLE);
                            xingShiZhengRl.setVisibility(View.GONE);
                            xingFile = saveFile;
                        } else if (selectZhengJianIv == jiaShiZhengIv) {
                            deleteJiaFl.setVisibility(View.VISIBLE);
                            jiaShiZhengRl.setVisibility(View.GONE);
                            jiaFile = saveFile;
                        }
                        Glide.with(CarOwnerAuthenticationActivity.this).load(saveFile).into(selectZhengJianIv);
                    }

                    @Override
                    public void onError(int code, String message) {
                        LogUtil.e(message);
                    }
                }, file);
                if (popupDialog1 != null && popupDialog1.isShowing()) {
                    popupDialog1.cancel();
                }
                break;

            case R.id.btn_cancel:
                if (popupDialog1 != null && popupDialog1.isShowing()) {
                    popupDialog1.cancel();
                }
                break;
        }
    }

    private void showChangePersonalIconDialog() {
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_select, null);
        popupDialog1 = new Dialog(this, R.style.dialog);
        popupDialog1.setContentView(view);
        Window dialogWindow = popupDialog1.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.BOTTOM);
        dialogWindow.setWindowAnimations(R.style.PopupWindowAnimation1);
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);
        popupDialog1.show();

        Button btn_take_photo = (Button) view.findViewById(R.id.btn_take_photo);
        btn_take_photo.setOnClickListener(this);
        Button btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
        btn_cancel.setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {

            case CameraUtil.REQUEST_CAMERA:
                cameraUtil.onHandleActivityResult(requestCode, resultCode, data);
                break;

            case CameraUtil.REQUEST_RESIZE:
                cameraUtil.onHandleActivityResult(requestCode, resultCode, data);
                break;

            default:
                break;

        }
        super.onActivityResult(requestCode, resultCode, data);
    }


    /* 上传头像 */
    public void uploadIcon(final String path) {
        Map<String, String> map = new HashMap<>();
        FileUpLoader.getInstance().uploadFile(path, "uploadFile", HttpUrl.getInstance().URL_042, map);
        FileUpLoader.getInstance().setOnUploadProcessListener(new FileUpLoader.OnUploadProcessListener() {
            @Override
            public void onUploadDone(int responseCode, String message) {
                LogUtil.i(responseCode + "-----" + message);
            }

            @Override
            public void onUploadProcess(int uploadSize) {
                LogUtil.i("+++++++" + uploadSize);
            }


            @Override
            public void initUpload(int fileSize) {
                LogUtil.i("======" + fileSize);
            }
        });
    }

    @Override
    protected boolean isClickable() {
        return false;
    }

    @Override
    protected void subtitleClick() {

    }

    @Override
    protected void titleClick() {

    }

    @Override
    protected void rightClick() {

    }

    @Override
    public String getCN() {
        return null;
    }

    @Override
    public void LogoutCallback(boolean islogin) {

    }
}
